#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
    int l,r,zhi;
}c[20020];
int t,n,ans;
int a[20010],ji[20],ne[1000020],dp[20020];
int calc(){
    int red=0,blue=0,cnt=0;
    for(int i=1;i<=n;i++){
        if(ji[i]==0){
            if(a[i]==a[red]) cnt+=a[i];
            red=i;
        }
        else{
            if(a[i]==a[blue]) cnt+=a[i];
            blue=i;
        }
    }
    return cnt;
}
void dfs(int dep){
    if(dep>n){
        ans=max(ans,calc());
        return;
    }
    ji[dep]=0; dfs(dep+1);
    ji[dep]=1; dfs(dep+1);
}
bool cmp(node x,node y){
    return x.l<y.l;
}
void work(){
    ans=0; cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    if(n<=15){
        dfs(1);
        cout<<ans<<'\n';
        return;
    }
    memset(dp,0,sizeof(dp));
    memset(ne,0,sizeof(ne));
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(ne[a[i]]==0) ne[a[i]]=i;
        else {
            c[++cnt]={ne[a[i]],i,a[i]};
            ne[a[i]]=0;
        }
    }
    sort(c+1,c+1+cnt,cmp);
    for(int i=1;i<=cnt;i++){
        dp[c[i].r]=c[i].zhi+c[i].l;
        ans=max(ans,dp[c[i].r]);
    }
    cout<<ans<<'\n';
    return;
}
signed main(){
   freopen("color.in","r",stdin);
   freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>t;
    while(t--){work();}
    return 0;
}
